<?php

namespace app\service;

use app\exception\ErrnoPlus;
use app\model\DescribeInfoModel;
use share\exception\ApiException;
use share\service\BaseModelService;
use share\utils\Helper;

class DescribeInfoService extends BaseModelService
{
    public static function getModel()
    {
        return new DescribeInfoModel;
    }
   

    public static function getByTitle($title){
        return self::getByFilter(['title'=>$title]);
    }

    public static function getByModelId($moduleId){
        return self::getByFilter(['model_id'=>$moduleId]);
    }

    public static function getAllModuleInfoByLanguage($param){
        $language = $param['language'];
        $allModule = ModuleInfoService::getAllByFilter([]);
        $datas = DescribeInfoService::getAllByFilter([
            'language' => $language,
            'describe_id' => ['in',array_column($allModule,'describe_id')],
        ]);
        $res = [];
        foreach($allModule as $module){
            $id = $module['id'];
            $describeInfo = Helper::filterOne($datas,['describe_id' =>  $module['describe_id']]);
            if($describeInfo){
                $res[] = [
                    'id' => $id,
                    'describe_name' => $describeInfo['describe_name'],
                ];
            }
        }
        return $res;
    }

    public static function addDescribe($param){        
        $item = Helper::arrayCamelKeyToUnderscore($param);
        if(!isset($item['describe_id'])){
            $item['describe_id'] = Helper::getUnique();
        }
        else{
            $origin = self::getByFilter([
                'describe_id' => $item['describe_id'],
                'language' => $item['language'],
            ]);
            if(!empty($origin)){
                throw new ApiException("the describe info already exist");
            }
        }
        return self::add($item);
    }

    public static function addSimple($param){
        if(isset($param['describeNameId'])){
            $param['describe_id'] = $param['describeNameId'];
            unset($param['describeNameId']);
        }
        return self::addDescribe($param);
    }

    public static function updateDescribe($param){        
        $id =  $param['id'];
        unset($param['id']);
        $origin = self::getBase($id);  
        if(empty($origin)){
            throw new ApiException("origin data is not exist",ErrnoPlus::COMMON_ERROR);
        }
        $item = Helper::arrayCamelKeyToUnderscore($param);
        if(isset($item['describe_id'])){ //  describe_id 不允许更改
            unset($item['describe_id']);
        }
        if(isset($param['language']) && $origin['language'] != $param['language']){
            $other = self::getByFilter([
                'describe_id' => $origin['describe_id'],
                'language' => $item['language'],
            ]);
            if(!empty($other)){
                throw new ApiException("the describe info already exist");
            }
        }
        
        $update = [];
        foreach($item as $key => $val){
            if($val != $origin[$key]){
                $update[$key] = $val;
            }
        }
        if(count($update) > 0){
            self::update($id,$update);
        }
    }

    public static function delDescribe($id){
        $origin = self::getBase($id);  
        if(empty($origin)){
            throw new ApiException("origin data is not exist",ErrnoPlus::COMMON_ERROR);
        }
        if(self::count(['describe_id' => $origin['describe_id']]) < 1){
            throw new ApiException("the last one can not be deleted",ErrnoPlus::COMMON_ERROR);
        }
        self::del($id);
    }

    public static function buildCondition($filter)
    {
        $res = [];
        if(isset($filter['describeInfoId']) && !empty($filter['describeInfoId'])){
            $res['describe_id'] = $filter['describeInfoId'];
        }
        return $res;
    }
}
